package cn.wolfcode;


import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.List;

public class Activititest {

//    @Test
//    public void test1(){
//        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
//        System.out.println(defaultProcessEngine);
//        System.out.println("打印成功");
//    }
//    //部署
//    @Test
//    public void testDeploy(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        //获取仓库 repositoryService
//        RepositoryService repositoryService = processEngine.getRepositoryService();
//       // 调用仓库repositoryService 中的API完成部署
//        Deployment deployment= repositoryService.createDeployment()
//                .addClasspathResource("bpmn/leaveProcess.bpmn20.xml")//需要部署什么资源
//                .deploy(); //进行部署动作
//        //部署对象只能获取到部署ID和部署时间
//        System.out.println("deployment.getId() = " + deployment.getId());
//        System.out.println("deployment.getDeploymentTime() = " + deployment.getDeploymentTime());
//    }
//
//    //发起流程实例
//    @Test
//    public  void startProcessInstance(){
//        //获取引擎对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        //获取运行时RuntimeService
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//        //调用APi来发起流程实力
//        ProcessInstance processInstance =
//                runtimeService.startProcessInstanceByKey("leaveProcess");
//        System.out.println("流程实例ID = " + processInstance.getId());
//        System.out.println("流程实例ID = " + processInstance.getProcessInstanceId());
//        System.out.println("流程实例对应的流程定义ID = " + processInstance.getProcessDefinitionId());
//        System.out.println("流程实例对应的流程定义Key = " + processInstance.getProcessDefinitionKey());
//
//    }
//
//
//    //查询代办任务
//    @Test
//    public void queryTest(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        TaskService taskService = processEngine.getTaskService();
//        String userId="张三";
//        List<Task> taskList= taskService.createTaskQuery()
//                .processDefinitionKey("leaveProcess")
//                .taskAssignee(userId)
//                .list();
//        for (Task task : taskList) {
//            System.out.println("任务ID = " + task.getId());
//            System.out.println("任务名称 = " + task.getName());
//            System.out.println("任务负责人 = " + task.getAssignee());
//            System.out.println("任务创建时间 = " + task.getCreateTime());
//            System.out.println("当前任务对应的流程定义ID = " + task.getProcessDefinitionId());
//            System.out.println("当前任务对应的流程实例ID = " + task.getProcessInstanceId());
//
//        }
//    }
//
//
//
//
//    //添加审批意见
//    @Test
//    public void testAddComment(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        TaskService taskService = processEngine.getTaskService();
//        String taskId="5005";
//        String processInstanceId="5002";
//        taskService.addComment(taskId,processInstanceId,"允许休假");
//        taskService.complete(taskId);
//        System.out.println("完成任务");
//    }
//
//
//
//
//    @Test
//    public void testQueryHistoryTask(){
//        // 获取流程引擎对象
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        // 获取历史 HistoryService
//        HistoryService historyService = processEngine.getHistoryService();
//        // 获取任务 taskService  用来查询历史批注信息（唯一使用 taskService 来查询的历史）
//        TaskService taskService = processEngine.getTaskService();
//        // 查询历史任务信息
//        // 历史流程实例
//        String processInstanceId = "5002";
//        // 查询历史任务
//        List<HistoricTaskInstance> historicTaskInstanceList = historyService.createHistoricTaskInstanceQuery()
//                .processInstanceId(processInstanceId)   // 过滤条件
//                .finished()     // 已完成任务
//                .list();
//        for (HistoricTaskInstance task : historicTaskInstanceList) {
//            System.out.println("任务ID：" + task.getId());
//            System.out.println("任务名称：" + task.getName());
//            System.out.println("任务负责人：" + task.getAssignee());
//            System.out.println("任务创建时间：" + task.getCreateTime().toLocaleString());
//            System.out.println("任务开始时间：" + task.getStartTime().toLocaleString());
//            System.out.println("任务结束时间：" + task.getEndTime().toLocaleString());
//            System.out.println("任务总耗时：" + task.getDurationInMillis()/1000 + "s");
//            System.out.println("当前任务对应的流程定义ID：" + task.getProcessDefinitionId());
//            System.out.println("当前任务对应的流程实例ID：" + task.getProcessInstanceId());
//            List<Comment> taskComments = taskService.getTaskComments(task.getId());
//            if(taskComments != null && taskComments.size() > 0){
//                for (Comment taskComment : taskComments) {
//                    // 打印批注信息
//                    System.out.println(taskComment.getFullMessage());
//                }
//            }
//            System.out.println("♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥");
//        }
//    }

    //初始化
@Test
public void testInit(){
    ProcessEngine processEngin = ProcessEngines.getDefaultProcessEngine();
    System.out.println(processEngin);
}

//部署
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/leaveProcess.bpmn20.xml")
                .deploy();
        System.out.println("部署ID = " + deployment.getId());
        System.out.println("部署time = " + deployment.getDeploymentTime());
    }

    //发起流程实例
    @Test
    public void startProcessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveProcess");
        System.out.println("流程实例ID = " + processInstance.getId());
        System.out.println("流程实例ID = " + processInstance.getProcessInstanceId());
        System.out.println("流程实例对应的流程定义ID = " + processInstance.getProcessDefinitionId());
        System.out.println("流程实例对应的流程定义Key = " + processInstance.getProcessDefinitionKey());
    }

    //查询代办任务
    @Test
    public void queryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String userId="张三";
        List<Task> taskQuery = taskService.createTaskQuery()
                .processDefinitionKey("leaveProcess")
                .taskAssignee(userId)
                .list();
        for (Task task : taskQuery) {
            System.out.println("任务ID = " + task.getId());
            System.out.println("任务名称 = " + task.getName());
            System.out.println("任务负责人 = " + task.getAssignee());
            System.out.println("任务创建时间 = " + task.getCreateTime());
            System.out.println("当前任务对应的流程定义ID = " + task.getProcessDefinitionId());
            System.out.println("当前任务对应的流程实例ID = " + task.getProcessInstanceId());
        }
    }

    //处理任务
    @Test
    public void completeTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId="12505";
        taskService.complete(taskId);
        System.out.println("完成任务");
    }

    //审批意见
    @Test
    public void testAddComment(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId="17502";
        String processInstanceId="12501";
        taskService.addComment(taskId,processInstanceId,"我也想休假");
        taskService.complete(taskId);
    }

    //查询历史任务信息
    @Test
    public  void testQueryHistoryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        TaskService taskService = processEngine.getTaskService();
        String processInstanced="5005";
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
//                .processInstanceId()
                .processInstanceId(processInstanced)
                .finished()
                .list();
        for (HistoricTaskInstance task : list) {
            System.out.println("任务ID = " + task.getId());
            System.out.println("任务名称 = " + task.getName());
            System.out.println("任务负责人 = " + task.getAssignee());
            System.out.println("任务创建时间 = " + task.getCreateTime().toLocaleString());
            System.out.println("任务开始时间 = " + task.getStartTime().toLocaleString());
            System.out.println("任务结束时间 = " + task.getEndTime().toLocaleString());
            System.out.println("任务总耗时 = " + task.getDurationInMillis()/1000+"s");
            System.out.println("当前任务对应的流程定义ID = " + task.getProcessDefinitionId());
            System.out.println("当前任务对应的流程实例ID = " + task.getProcessInstanceId());
            List<Comment> taskComments = taskService.getTaskComments(task.getId());
            if(taskComments !=null && taskComments.size()>0){
                for (Comment taskComment : taskComments) {
                    System.out.println(taskComment.getFullMessage());
                }
            }
            System.out.println("爱心");
        }
    }

}
